package com.yiyi.credit.manage.abutment.tdapi.controller;


import com.alibaba.fastjson.JSONObject;
import com.yiyi.credit.manage.abutment.common.bean.APIResponse;
import com.yiyi.credit.manage.abutment.common.enums.*;
import com.yiyi.credit.manage.abutment.common.utils.SignatureUtils;
import com.yiyi.credit.manage.abutment.service.CcxiService;
import com.yiyi.credit.manage.abutment.service.TdService;
import com.yiyi.credit.manage.model.entity.InterfaceFlowTd;
import com.yiyi.credit.manage.model.vo.AntifraudVo;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/td")
public class TdController {

    public static final Logger LOGGER = LoggerFactory.getLogger(TdController.class);

    @Value("${td.partner_code}")
    private String account;

    @Autowired
    private TdService tdService;

    @Autowired
    private CcxiService ccxiService;

    @ApiOperation(value = "贷前反欺诈", notes = "贷前反欺诈", response = APIResponse.class)
    @PostMapping("/antifraud")
    public APIResponse antifraud(@RequestBody @Valid AntifraudVo vo, BindingResult result) throws Exception {

        long start = System.currentTimeMillis();
        String reqDate = JSONObject.toJSONString(vo);
        LOGGER.info("{}平台请求参数:{}",vo.getAppId(),reqDate);

        if(result.hasErrors()){
            return new APIResponse(ResponseCode.PARAM_ERR.getCode(),result.getFieldError().getDefaultMessage());
        }
        String appid = vo.getAppId();
        String privateKey = AppIdEnum.getPrivateKeyByCode(appid);
        if(privateKey==null) return new APIResponse<>(ResponseCode.NOT_APPID);

        boolean bool= SignatureUtils.checkSign(reqDate,privateKey);
        if(!bool) return new APIResponse<>(ResponseCode.SIGN_ERR);

        List<InterfaceFlowTd> list=tdService.uniqueRequestNo(appid,vo.getRequestNo());
        if(list.size()>0) return new APIResponse<>(ResponseCode.REQUESTNO_REPEAT, vo.getRequestNo());

        Map<String, Object> map = null;
        map = tdService.antifraud(reqDate,appid,vo.getRequestNo());

        String respStr = map.get("respStr").toString();
        String code = map.get("code").toString();
        String msg = map.get("msg").toString();
        String requestNo = map.get("requestNo").toString();
        JSONObject data = null;
        if (map.get("data")!=null) data=(JSONObject) map.get("data");

        if("0000".equals(code) )
        ccxiService.updateProjectToThirdCost(appid, account, SpendEnum.ANTIFRAUD_SPEND.getValue(), TdInterfacePathEnum.ANTIFRAUD.getPath());

        LOGGER.info("接口耗时:{}ms ",System.currentTimeMillis()-start);
        return new APIResponse<>(data,code,msg,requestNo);
    }
    }

